package frame;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import bean.BusType;
import dao.BusStopDaoImpl;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class BusInforChanged {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;

    String[] selectedData = null;	//接收表格中选中的数据

    JCheckBox checkBox_3;
    JCheckBox checkBox_3_1;
    JCheckBox checkBox_3_2;
    JCheckBox checkBox_3_3;
    JCheckBox checkBox_3_4;
    JCheckBox checkBox_3_5;
    JCheckBox checkBox_3_6;
    JCheckBox checkBox_3_7;
    JComboBox comboBox;
    private JComboBox comboBox_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BusInforChanged window = new BusInforChanged();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public BusInforChanged() {
        initialize();
    }

    public  BusInforChanged(String[] receiveData) {
        this.selectedData = new String[100];
        for(int i=0; i<receiveData.length; i++) {
            this.selectedData[i] = receiveData[i];
        }
        initialize();
    }
    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 592, 500);
        frame.setLocationRelativeTo(null); // 默认居中显示
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        frame.setVisible(true);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, 572, 453);
        frame.getContentPane().add(panel);

        JLabel label = new JLabel("\u516C\u4EA4\u8F66\u4FE1\u606F\u4FEE\u6539");
        label.setFont(new Font("宋体", Font.PLAIN, 30));
        label.setBounds(181, 13, 210, 35);
        panel.add(label);

        JLabel label_1 = new JLabel("\u53F8\u673A\u7F16\u53F7\uFF1A");
        label_1.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1.setBounds(45, 86, 117, 31);
        panel.add(label_1);

        textField = new JTextField();
        textField.setColumns(10);
        textField.setBounds(181, 91, 86, 24);
        panel.add(textField);
        textField.setText(selectedData[1]);


        JLabel label_1_1 = new JLabel("\u8F66\u724C\u53F7\uFF1A");
        label_1_1.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1_1.setBounds(45, 145, 117, 31);
        panel.add(label_1_1);

        JLabel label_1_2 = new JLabel("\u516C\u4EA4\u8F66\u7C7B\u578B\uFF1A");
        label_1_2.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1_2.setBounds(45, 201, 128, 31);
        panel.add(label_1_2);

        JLabel label_1_3 = new JLabel("\u516C\u4EA4\u8F66\u7AD9\uFF1A");
        label_1_3.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1_3.setBounds(45, 263, 117, 31);
        panel.add(label_1_3);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(181, 150, 86, 24);
        panel.add(textField_1);
        textField_1.setText(selectedData[2]);

        comboBox_1 = new JComboBox();
        comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"\u666E\u901A\u516C\u4EA4", "\u7535\u8F66", "\u53CC\u5C42\u5DF4\u58EB"}));
        comboBox_1.setBounds(181, 206, 86, 24);
        panel.add(comboBox_1);
        comboBox_1.setSelectedItem(selectedData[3]);

        checkBox_3 = new JCheckBox("\u9C7C\u5C71\u8DEF");
        checkBox_3.setBounds(167, 267, 77, 27);
        panel.add(checkBox_3);


        checkBox_3_1 = new JCheckBox("\u5C71\u4E1C\u8DEF");
        checkBox_3_1.setBounds(244, 267, 77, 27);
        panel.add(checkBox_3_1);

        checkBox_3_2 = new JCheckBox("\u4E5D\u6C5F\u8DEF");
        checkBox_3_2.setBounds(327, 267, 77, 27);
        panel.add(checkBox_3_2);

        checkBox_3_3 = new JCheckBox("\u71D5\u513F\u5C9B\u8DEF");
        checkBox_3_3.setBounds(28, 303, 100, 27);
        panel.add(checkBox_3_3);

        checkBox_3_4 = new JCheckBox("\u5927\u5B66\u8DEF");
        checkBox_3_4.setBounds(128, 303, 77, 27);
        panel.add(checkBox_3_4);

        checkBox_3_5 = new JCheckBox("\u4EBA\u6C11\u8DEF");
        checkBox_3_5.setBounds(211, 303, 77, 27);
        panel.add(checkBox_3_5);

        checkBox_3_6 = new JCheckBox("\u5EB7\u5B81\u8DEF");
        checkBox_3_6.setBounds(294, 303, 77, 27);
        panel.add(checkBox_3_6);

        checkBox_3_7 = new JCheckBox("\u5468\u53E3\u8DEF");
        checkBox_3_7.setBounds(387, 303, 77, 27);
        panel.add(checkBox_3_7);

        JButton button = new JButton("\u4FEE\u6539\u4FE1\u606F");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int b =JOptionPane.showConfirmDialog(frame, "确认修改吗？");
                if(b == 0) {
                    BusType bus = new BusType();
                    JTextField[] jtf= {textField,textField_1};
                    //检测空的文本框，将空的标红,并提示输入完整
                    int i = 0;
                    for(; i<jtf.length; i++) {
                        if(jtf[i].getText().equals("")) {
                            jtf[i].setBorder(BorderFactory.createLineBorder(Color.RED)); //将边框标红
                            break;
                        }
                    }
                    if(i == jtf.length) {
                        String did = textField.getText();
                        String bnum = textField_1.getText();
                        String btype = comboBox_1.getSelectedItem()+"";	//获得comboBox当前选中值
                        String busstop="";
                        if(checkBox_3.isSelected()){
                            busstop+=checkBox_3.getText()+"-";
                        }
                        if(checkBox_3_1.isSelected()){
                            busstop+=checkBox_3_1.getText()+"-";
                        }
                        if(checkBox_3_2.isSelected()){
                            busstop+=checkBox_3_2.getText()+"-";
                        }
                        if(checkBox_3_3.isSelected()){
                            busstop+=checkBox_3_3.getText()+"-";
                        }
                        if(checkBox_3_4.isSelected()){
                            busstop+=checkBox_3_4.getText()+"-";
                        }
                        if(checkBox_3_5.isSelected()){
                            busstop+=checkBox_3_5.getText()+"-";
                        }
                        if(checkBox_3_6.isSelected()){
                            busstop+=checkBox_3_6.getText()+"-";
                        }
                        if(checkBox_3_7.isSelected()){
                            busstop+=checkBox_3_7.getText()+"-";
                        }
                        bus.setDid(Integer.parseInt(did));
                        bus.setBnum(bnum);
                        bus.setBtype(btype);
                        bus.setBusstop(busstop);

                        int j = new BusStopDaoImpl().updateBus(bus);

                        if(j!=0) {
                            JOptionPane.showMessageDialog(frame, "修改成功！");
                            //成功后关闭页面
                            frame.setVisible(false);
                        }else{
                            System.out.println(did+bnum+btype+busstop);
                        }
                    }
                }
            }
        });
        button.setBounds(229, 382, 113, 27);
        panel.add(button);


    }
}